<!DOCTYPE html>
<html lang="cn" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <link rel="icon" href="/web页面/logo3.png" type="image/png">

    <meta charset="UTF-8">
    <title>商城</title>
    <script src="axios.min.js"></script>
    <script src="vue.global.js"></script>
    <style>

        body {
            font-family: Arial, sans-serif;
            background-color: rgb(238, 232, 232);
            overflow-y: scroll; /* 如果需要始终显示滚动条 */
        }

        h3 {
            text-align: center;
            margin-top: 20px;
            font-size: 24px;
        }

        img {
            border-radius: 4px;
            width: 380px; /* 图片宽度调整 */
            height: 380px; /* 图片高度调整 */
        }

        .goods {
            display: flex; /* 使用 Flexbox 布局 */
            flex-direction: column; /* 子元素垂直排列 */
            justify-content: space-between; /* 在子元素之间分配空间 */
            align-items: center; /* 子元素水平居中 */
            background-color: white;
            width: 23.5%; /* 每个商品占据页面宽度的五分之一 */
            box-sizing: border-box;
            margin: 10px; /* 商品之间的间距 */
            text-align: center;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
            min-height: 400px; /* 设置最小高度，确保内容不会挤压 */
        }


        .goods:hover {
            transform: scale(1.05);
        }

        .goodspic img {
            max-width: 70%;
            max-height: 100%;
            height: auto;
            border-radius: 8px;
        }

        .goods span {
            position: relative; /* 使用相对定位 */
            top: -30px; /* 向上移动20px */
            margin-bottom: 0; /* 移除底部边距 */
        }

        /* 美化按钮 */
        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s, transform 0.3s;
        }

        button:hover {
            background-color: #0056b3;
            transform: scale(1.05);
        }

        button:focus {
            outline: none;
        }

        .goods div:first-child {
            position: relative; /* 使用相对定位 */
            top: 20px; /* 向下移动20px */
        }
        a {
            text-decoration: none;
            padding: 10px 20px;
            background-color: #fb6901;
            color: white;
            border-radius: 5px;
            font-size: 16px;
            transition: background-color 0.3s, transform 0.3s;
        }
        .button-group a {
            position: relative; /* 使用相对定位 */
            top: -20px; /* 向上移动30px */
            width: 100px; /* 增加按钮宽度 */
            text-align: center; /* 确保文字居中 */
        }

        a:hover {
            background-color: #fc6900;
            transform: scale(1.05);
        }

        .category-links {
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
            margin-bottom: 20px;
        }

        /* 商品类别链接样式 */
        .category-links span {
            display: inline-block;
            padding: 10px 20px;
            margin: 5px;
            background-color: #ffffff;
            border: 1px solid #ddd;
            border-radius: 25px;
            font-size: 18px;
            cursor: pointer;
            color: #333;
            height: 30px;
            line-height: 30px;
            transition: background-color 0.3s, color 0.3s, transform 0.3s;
        }

        /* 鼠标悬停时的类别链接样式 */
        .category-links span:hover {
            background-color: #8ebef1;
            color: white;
            transform: scale(1.05);
        }

        /* 商品列表容器样式 */
        .product-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-start;  /* 不居中，左对齐 */
            padding: 20px;
            background-color: rgba(242, 242, 242, 0.95);
        }
        .goods .goodspic {
            margin-bottom: 10px; /* 增加图片和价格之间的间距 */
        }

        .goods span {
            margin-bottom: 15px; /* 增加价格和按钮之间的间距 */
        }
        /* 添加按钮的容器样式 */
        .button-group {
            display: flex;
            justify-content: center;
            gap: 10px; /* 按钮之间的间距 */
        }

        .nav-btn {
            display: inline-block;
            margin: 5px;
            padding: 5px 20px;
            color: white;
            border: none;
            border-radius: 25px; /* 圆角按钮 */
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s, transform 0.3s, box-shadow 0.3s;
            text-align: center;

        }

        .nav-btn:hover {
            transform: translateY(-2px); /* 悬停时轻微上移 */
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); /* 添加阴影效果 */
        }

        .nav-btn:active {
            transform: translateY(0); /* 点击时取消上移效果 */
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); /* 减弱阴影效果 */
        }


        /* 链接样式 */
        .nav-btn a {
            color: white; /* 链接颜色 */
            text-decoration: none; /* 移除下划线 */
            display: block; /* 使链接填充整个按钮 */
        }
        .logo image{
            height: 50px; /* 或者你想要的高度 */
            width: 50px; /* 或者你想要的宽度 */
        }
    </style>
</head>
<body>
<div id="app">


    <script>
        document.addEventListener("DOMContentLoaded", function() {
            // 发送 AJAX 请求到服务器以检查用户是否登录
            axios.get('/check-login-status')
                .then(function(response) {
                    // 假设服务器返回一个对象，其中包含用户ID
                    var user_id = response.data.user_id;
                    var nickname = response.data.nickname;

                    // 查找登录和注册按钮的容器
                    var loginRegisterContainer = document.getElementById('login-register-container');
                    var registerContainer = document.getElementById('register-container');
                    var cartBtn = document.getElementById('cart-btn');
                    var orderBtn = document.getElementById('order-btn');

                    // 如果用户已登录，则隐藏登录和注册按钮，显示购物车和订单按钮
                    if (user_id) {
                        loginRegisterContainer.style.display = 'none';
                        registerContainer.style.display = 'none';

                        cartBtn.style.display = 'inline-block'; // 显示购物车按钮
                        orderBtn.style.display = 'inline-block'; // 显示订单按钮
                    } else {
                        // 如果用户未登录，则显示登录和注册按钮
                        loginRegisterContainer.style.display = 'inline-block';
                        registerContainer.style.display = 'inline-block';

                        cartBtn.style.display = 'none'; // 隐藏购物车按钮
                        orderBtn.style.display = 'none'; // 隐藏订单按钮
                    }
                })
                .catch(function(error) {
                    console.error('Error checking login status:', error);
                });
        });
    </script>

    <div class="category-links">
        <div class="nav-btn"  ><a href="home.html">首页</a></div>
        <span v-on:click="getGoodsByTypesId(1)">手机</span>
        <span v-on:click="getGoodsByTypesId(2)">平板</span>
        <span v-on:click="getGoodsByTypesId(3)">家电</span>
        <span v-on:click="getGoodsByTypesId(4)">笔记本</span>
        <span v-on:click="getGoodsByTypesId(5)">穿戴</span>
        <span v-on:click="getGoodsByTypesId(6)">电源配件</span>
        <div class="nav-btn" id="login-register-container" ><a href="http://127.0.0.1:8080/login2.html" target="_blank">登录</a></div>
        <div class="nav-btn" id="register-container" ><a href="http://127.0.0.1:8080/registration2.html" target="_blank">注册</a></div>

        <!-- 购物车和订单按钮，默认不显示 -->
        <div class="nav-btn" id="cart-btn" style="display: none;"><a href="/carts.html" target="_blank">购物车</a></div>
        <div class="nav-btn" id="order-btn" style="display: none;"><a href="/orders.html" target="_blank">我的订单</a></div>

    </div>

    <div class="product-container">
        <div class="goods" v-for="goods in goodsList">
            <div v-text="goods.name"></div>
            <div class="goodspic">
                <img v-bind:src="goods.pic" width="500" height="500">
            </div>
            <div>
                <span v-text="'￥'+goods.price" style="color: red; font-weight: 800"></span>
            </div>
            <div class="button-group">
                <a v-bind:href="'http://127.0.0.1:8080/xq.html?id='+goods.id" target="_blank">详情</a>
            </div>
        </div>
        </div>
    </div>
</div>

<script>
    const app = {
        data() {
            return {
                goodsList: []
            }
        },
        methods: {
            getGoodsByTypesId: function (types_id) {
                console.log("开始获取商品！")
                axios({
                    method: 'post',
                    url: '/getGoodsByTypesId',
                    data: {
                        types_id: types_id
                    }
                }).then((response) => {
                    this.goodsList = response.data
                    console.log("获取数据成功")
                    console.log(this.goodsList)
                })
            }
        },
        mounted() {
            console.log("vue启动成功！")
            const urlParams = new URLSearchParams(window.location.search);
            const category = urlParams.get('category');  // 获取查询参数中的 category

            if (category) {
                this.category = category;  // 设置类别名称
                this.getGoodsByTypesId(category);  // 调用方法获取商品
            } else {
                console.error("未找到有效的商品类别！");
            }
        }
    }
    Vue.createApp(app).mount("#app")
</script>
</body>
</html>
